package sk.vrto.domain;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import java.io.Serializable;

/**
 * Contact for email user.
 */
@Entity
@Table(name = "CONTACTS")
public class Contact implements Serializable {

    @Id
    private String email;
    private String name;

    @OneToOne
    private User user;

    /**
     * Creates new contact
     * @param name The name of the contact (any formal/informal value is ok)
     * @param email Contact's valid email address
     * @param user Owner of the contact
     */
    public Contact(String name, String email, User user) {
        this.name = name;
        this.email = email;
        this.user = user;
    }

    public Contact() {
        // hibernate ctor
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    @Override
    public String toString() {
        return "Contact{" +
                "name='" + name + '\'' +
                ", email='" + email + '\'' +
                ", user=" + user +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        Contact contact = (Contact) o;

        if (email != null ? !email.equals(contact.email) : contact.email != null) {
            return false;
        }
        if (name != null ? !name.equals(contact.name) : contact.name != null) {
            return false;
        }

        return true;
    }

    @Override
    public int hashCode() {
        int result = name != null ? name.hashCode() : 0;
        result = 31 * result + (email != null ? email.hashCode() : 0);
        return result;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }
}
